package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.StoreFollowType;
import com.dingreading.cloud.admin.service.StoreFollowService;
import com.dingreading.cloud.admin.service.StoreFollowTypeService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.KitUtil;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RefreshScope
@RestController
@RequestMapping("/system/storeFollowType")
@Api(tags = "门店跟进类型管理")
public class StoreFollowTypeController {

    @Resource
    private StoreFollowTypeService followTypeService;
    @Resource
    private StoreFollowService followService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "类型") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "来源 1.系统 2.员工") @RequestParam(value = "source", required = false) Integer source,
            @ApiParam(value = "业务展示 0.不展示 1.展示") @RequestParam(value = "operate", required = false) Integer operate
    ) {
        Page<StoreFollowType> page = followTypeService.pageList(pageUtil, keyword, source, operate);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }

    @ApiOperation(value = "根据id获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        StoreFollowType data = followTypeService.getById(id);
        if (null == data)
            return R.fail("未查询到信息");

        return R.ok(data);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "创建/更新信息")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "类型名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "业务展示 0.不展示 1.展示") @RequestParam(value = "operation") Integer operation,
            @ApiParam(value = "来源 1.系统 2.员工") @RequestParam(value = "source") Integer source
    ) throws Exception {
        if (StringUtils.isBlank(name))
            return R.fail("请输入类型名称");
        if (operation == null)
            return R.fail("请选择业务展示");

        boolean b = followTypeService.haveName(id, name);
        if (b)
            return R.fail("跟进名称重复了");

        StoreFollowType data = null;
        if (null == id || id <= 0) {
            data = new StoreFollowType();
            data.setUid(KitUtil.getUuid());
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
        } else {
            data = followTypeService.getById(id);
            if (null == data)
                return R.fail("未查询到合同");
            boolean used = followService.used(data.getUid());
            if (used)
                return R.fail("已被使用不能操作");
        }
        data.setName(name);
        data.setOperation(operation);
        data.setSource(source);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        followTypeService.saveOrUpdate(data);

        return R.ok();
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/deleteBean", produces = "application/json; charset=UTF-8")
    public R<Object> deleteBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        StoreFollowType data = followTypeService.getById(id);
        if (data == null)
            return R.fail("未查询到信息");

        boolean used = followService.used(data.getUid());
        if (used)
            return R.fail("已被使用不能删除");

        boolean b = followTypeService.removeById(id);
        if (!b)
            return R.failDel();

        return R.ok();
    }

}